package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Override
    public List<NursingPlanVo> getAll() {
        return nursingPlanMapper.selectNursingPlanAll();
    }

    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        //重名校验
        int num = nursingPlanMapper.countByName(nursingPlan.getPlanName());
        if (num > 0) {
            throw new BaseException(BasicEnum.DUPLICATE_NURSING_PLAN);
        }

        nursingPlan.setStatus(1);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlanMapper.insert(nursingPlan);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingPlan.getProjectPlans();
        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(nursingProjectPlanVos, NursingProjectPlan.class);
        nursingProjectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));

        nursingProjectPlanMapper.insert(nursingProjectPlans);

    }

    @Override
    public PageResponse selectByPage(Integer pageNum, Integer pageSize, String planName, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> page = nursingPlanMapper.selectPageByNameAndStatus(planName, status);
        return PageResponse.of(page, NursingPlan.class);
    }

    @Override
    public NursingPlanVo selectById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);

        List<NursingProjectPlanVo> nursingProjectPlanVos =nursingProjectPlanMapper.selectByPlanId(nursingPlan.getId());

        nursingPlan.setProjectPlans(nursingProjectPlanVos);

        return BeanUtil.copyProperties(nursingPlan, NursingPlanVo.class);
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);

        nursingPlanMapper.update(nursingPlan);
        if (nursingPlan.getProjectPlans()!=null && !nursingPlan.getProjectPlans().isEmpty()) {
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            List<NursingProjectPlanVo> nursingProjectPlanVos = nursingPlan.getProjectPlans();
            nursingProjectPlanVos.forEach(nursingProjectPlanVo -> nursingProjectPlanVo.setPlanId(nursingPlan.getId()));
            List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(nursingProjectPlanVos, NursingProjectPlan.class);
            nursingProjectPlanMapper.insert(nursingProjectPlans);
        }
    }

    @Override
    public void deleteById(Long id) {

        nursingPlanMapper.deleteById(id);
        nursingProjectPlanMapper.deleteByPlanId(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }
}
